﻿//
//  HFHttpLJCFactory.m
//  HFBaseLibModule
//
//  Created by Points on 2020/4/3.
//  Copyright © 2020 hfqf123@126.com. All rights reserved.
//

#import "HFHttpLJCSignFactory.h"

@interface HFHttpLJCSignFactory()

@end

@implementation HFHttpLJCSignFactory

- (id<HFHttpAbstractFactory> (^)(void))post{
    
    __block __weak typeof(self) weakSelf = self;
    return ^(void){
        if (self.rsaEncryptResult) {
            BOOL needToast = weakSelf.needToast;
            BOOL rsaSwitch = weakSelf.rsaSwitch;
            HFHttpSucceedBlock successBlock = weakSelf.mSucceedBlock;
            HFHttpFailedBlock failBlock = weakSelf.mFailedBlock;

            if (HFBaseHTTPRequestManager.manager.signEnabled) {
                NSDate *date = NSDate.date;
                NSTimeInterval timeInterval = [date timeIntervalSince1970];
                long long milliseconds = timeInterval * 1000;
                NSInteger random = [HFBaseHTTPRequestTool randomFromA:1000 toB:9999];
                long long sign = [NSString stringWithFormat:@"%ld%lld", (long)random, milliseconds].longLongValue;
                [HFBaseHTTPRequestManager.manager.ljcSignRequestSerializer setValue:[NSString stringWithFormat:@"%llx", sign] forHTTPHeaderField:@"sign"];
            }
            
            NSDictionary *para = weakSelf.mPara;
            NSURLSessionTask *task = [HFBaseHTTPRequestManager LJCSignPOST:weakSelf.mPath parameters:weakSelf.mPara success:^(NSURLSessionDataTask *task, id responseObject) {
                [HFHttpLJCSignFactory requestSuccessWithTask:task
                                                         body:para
                                               responseObject:responseObject
                                               successHandler:successBlock
                                                    needToast:needToast
                                                    rsaSwitch:rsaSwitch];
            } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
                [HFHttpLJCSignFactory requestFailureWithTask:task
                                                        param:weakSelf.mPara
                                                        error:error
                                               failureHandler:failBlock
                                                    needToast:needToast];
            }];
            [weakSelf startDelayTimer:task];
        }
      
        return weakSelf;
    };
}

@end

